#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int calcComplex(lua_State *L)
{
    double r = luaL_checknumber(L, 1);
    double i = luaL_checknumber(L, 2);
    double c = r+i; 
    double d = r-i;
    
    lua_pushnumber(L, c);
    lua_pushnumber(L, d);
    
    return 2;
}

int main()
{
    char *szLua_code = "v, a = CalcComplex(4, 3) "
                       "print(v, a)";

    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    lua_pushcfunction(L, calcComplex);
    lua_setglobal(L, "CalcComplex");

    int err = luaL_loadstring(L, szLua_code) || lua_pcall(L,0,0,0);
    if (err)
    {
        fprintf(stderr, "%s\n", lua_tostring(L, -1));
        lua_pop(L, 1);
    }
    
    lua_close(L);
    return 0;
}
